import request from "@/utils/request";
import type {
  GetUserListParams,
  GetUserListResponse,
  UserItem,
  GetRoleByUserIdResponse,
} from "./models/userModels";

enum Api {
  getUserListUrl = `/admin/acl/user`,
  addUserUrl = `/admin/acl/user/save`,
  updateUserUrl = `/admin/acl/user/update`,
  removeUserUrl = `/admin/acl/user/remove`,
  getRoleByUserIdUrl = `/admin/acl/user/toAssign`,
  assignUserRoleListUrl = `/admin/acl/user/doAssignRole`,
  batchRemoveUserListUrl = `/admin/acl/user/batchRemove`,
}

/**
 * 获取用户列表
 * @param GetUserListParams
 * @returns GetUserListResponse
 */
export const getUserListApi = ({
  page,
  limit,
  username,
  nickName,
}: GetUserListParams) => {
  return request.get<any, GetUserListResponse>(
    Api.getUserListUrl + `/${page}/${limit}`,
    {
      params: {
        username,
        nickName,
      },
    }
  );
};

/**
 * 添加用户
 * @param user UserItem
 * @returns null
 */
export const addUserApi = (user: UserItem) => {
  return request.post<any, null>(Api.addUserUrl, user);
};

/**
 * 更新用户
 * @param user UserItem
 * @returns null
 */
export const updateUserApi = (user: UserItem) => {
  return request.put<any, null>(Api.updateUserUrl, user);
};

/**
 * 删除用户
 * @param id 用户id
 * @returns null
 */
export const removeUserApi = (id: string) => {
  return request.delete<any, null>(Api.removeUserUrl + `/${id}`);
};

/**
 * 根据用户id获取角色列表
 * @param id 用户id
 * @returns GetRoleByUserIdResponse
 */
export const getRoleByUserIdApi = (id: string) => {
  return request.get<any, GetRoleByUserIdResponse>(
    Api.getRoleByUserIdUrl + `/${id}`
  );
};

/**
 * 给用户分配角色列表
 * @param userId 用户id
 * @param roleIdList 角色列表
 * @returns null
 */
export const assignUserRoleListApi = (userId: string, roleIdList: string[]) => {
  return request.post<any, null>(Api.assignUserRoleListUrl, {
    userId,
    roleIdList,
  });
};

/**
 * 批量删除用户列表
 * @param idList 用户id列表
 * @returns null
 */
export const batchRemoveUserListApi = (idList: string[]) => {
  return request.post<any, null>(Api.batchRemoveUserListUrl, idList);
};
